package com.fast.common.db.configuration;

import com.alibaba.nacos.api.exception.NacosException;
import com.google.common.base.Strings;
import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereURLProvider;

import java.nio.charset.StandardCharsets;
public class NacosShardingSphereURLProvider implements ShardingSphereURLProvider {

    private static final String PATH_TYPE = "nacos:";


    @Override
    public boolean accept(String url) {
        return !Strings.isNullOrEmpty(url) && url.contains(PATH_TYPE);
    }

    @Override
    public byte[] getContent(String url, String urlPrefix) {
        String dataId = url.substring(urlPrefix.length()).substring(PATH_TYPE.length());
        try {
            return NacosConfigHolder.nacosConfigManager.getConfigService().getConfig(dataId, NacosConfigHolder.nacosConfigManager.getNacosConfigProperties().getGroup(),3000).getBytes(StandardCharsets.UTF_8);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }
}
